############################################################
#
# This file should only contain CFLAGS_XXX and LDFLAGS_XXX directives.
# CFLAGS and LDFLAGS themselves should NOT be set: that is the job
# for the actual Makefiles (which will combine the flags given here)
#
# *** DO NOT SET CFLAGS or LDFLAGS  ***
#
# Our recommended flags for all projects. Note -pthread specifies reentrancy

# -Wno-format-zero-length: permit printf("");
# -Wno-unused-parameter: permit a function to ignore an argument
CFLAGS_STD   := -std=gnu99 -g -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_REENTRANT -Wall -Wno-unused-parameter -Wno-format-zero-length -pthread
LDFLAGS_STD  := -lm

ROOT_PATH    := $(shell pwd)/../..
SRC_PATH     := $(ROOT_PATH)/src
BIN_PATH     := $(ROOT_PATH)/bin
LIB_PATH     := $(ROOT_PATH)/lib
CONFIG_DIR   := $(shell pwd)/../../config

# Don't set these here so we can more easily cross compile
#CC           := gcc
#LD           := gcc

# dynamic libraries
ifeq "$(shell uname -s)" "Darwin"
	LDSH := -dynamic
	SHEXT := .dylib
	WHOLE_ARCHIVE_START := -all_load
else
	LD := gcc
	LDSH := -shared
	SHEXT := .so
	WHOLE_ARCHIVE_START := -Wl,-whole-archive
	WHOLE_ARCHIVE_STOP := -Wl,-no-whole-archive
endif

############################################################
#
# External libraries
#
# List these in roughly the order of dependency; those with fewest
# dependencies first. Within each LDFLAGS, list the dependencies in in
# decreasing order (e.g., end with LDFLAGS_GLIB)
#
############################################################

# common library
CFLAGS_COMMON  := -I$(SRC_PATH) -DCONFIG_DIR='"$(CONFIG_DIR)"'
LDFLAGS_COMMON := $(LIB_PATH)/libcommon.a

# glib
CFLAGS_GLIB  := `pkg-config --cflags glib-2.0 gmodule-2.0`
LDFLAGS_GLIB := `pkg-config --libs glib-2.0 gmodule-2.0 gthread-2.0 gobject-2.0`

# gsl (GNU scientific libraries)
CFLAGS_GSL   := -DHAVE_INLINE `gsl-config --cflags`
LDFLAGS_GSL  := `gsl-config --libs`

# jpeg
ifeq "$(shell test -f /usr/lib/libjpeg-ipp.so -o -f /usr/lib64/libjpeg-ipp.so && echo ipp)" "ipp"
	LDFLAGS_JPEG := -ljpeg-ipp
else
	LDFLAGS_JPEG := -ljpeg
endif

# gtk
CFLAGS_GTK   :=`pkg-config --cflags gtk+-2.0`
LDFLAGS_GTK  :=`pkg-config --libs gtk+-2.0 gthread-2.0`

CFLAGS_GTK_UTIL  := -I$(SRC_PATH) $(CFLAGS_GTK)
LDFLAGS_GTK_UTIL := $(LIB_PATH)/libgtk_util.a $(LDFLAGS_GTK)

# glade target path should be relative to one directory deep from common.mk
GLADE_TARGET_PATH:=$(BIN_PATH)/glade
CFLAGS_GLADE:=-DGLADE_TARGET_PATH='"$(GLADE_TARGET_PATH)"' \
	`pkg-config --cflags libglade-2.0`
LDFLAGS_GLADE:=`pkg-config --libs libglade-2.0` -rdynamic

################  LCM  ###############
CFLAGS_LCM  := `pkg-config --cflags lcm`
LDFLAGS_LCM := `pkg-config --libs lcm`

# project lcmtypes
CFLAGS_LCMTYPES  := -I$(SRC_PATH)
LDFLAGS_LCMTYPES := $(LIB_PATH)/liblcmtypes.a

################ UROC  ###############
CFLAGS_UORC  := -I$(SRC_PATH)
LDFLAGS_UORC := $(LIB_PATH)/libuorc.a

################################################
%.o: %.c %.h
	@echo "    [$@]"
	$(CC) $(CFLAGS) -c $<

%.o: %.c
	@echo "    [$@]"
	$(CC) $(CFLAGS) -c $<
%.o: %.cpp
	@echo "    [$@]"
	g++ -c -o $@ $< $(CFLAGS_CXX)


